<template>
	<view>
		<!-- 已有地址列表 -->
		<view class="address" v-for="item in siteList" :key="item.addId" >
			<van-swipe-cell right-width="65" @click.stop="handleAddress(item)">
				<view class="content" >
					<view class="left">
						<view class="top">
							<view class="name">{{item.name}}</view>
							<view class="phone">{{item.phone}}</view>
						</view>
						<view class="bot">{{item.province}} {{item.city}} {{item.district}} {{item.address}}</view>
					</view>
				</view>
				<view slot="right" class="delete" @click.stop="deleteAddress(item.addId)"><u-button type="error" text="删除"></u-button></view>
			</van-swipe-cell>
		</view>
		<!-- 新增地址按钮 -->
		<view class="addAddress" @click="addAddress">
			<view class="add">
				<u-icon name="plus-circle" color="#AEB5A5" class="icon" :size="20"></u-icon>
				<text>添加地址</text>
			</view>
		</view>
		<!-- 空内容提示 -->
		<u-empty text="暂无收货地址" mode="favor" margin-top="200" v-if="!siteList.length"></u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 地址列表
				siteList: [],
				beforeRoute: '',
				index: ''
			}
		},
		onShow() {
			this.getAddressList();
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			this.beforeRoute = routes[routes.length - 2].route //获取上一页的页面路由
		},
		onLoad(options) {
			this.index = options.index;
		},
		methods: {
			//获取地址列表
			async getAddressList() {
				const {data:res} = await this.$myRequest({
					url: '/addressControllerBefore/listAddress/'+uni.getStorageSync('userId')
				})
				this.siteList = res.data.address || [];
			},
			// 修改地址信息或携带地址给发布信息页面
			handleAddress(item) {
				if (this.beforeRoute == 'pages/Mine/Me') {
					console.log(item);
					uni.navigateTo({
						url: '/page_mine/EditAddress?addressInfo=' + encodeURIComponent(JSON.stringify(item))
					})
				} else {
					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 2]; //上一个页面
					let attr = {
						res: '',
						index: ''
					}
					attr.res = item
					attr.index = this.index
					let info = encodeURIComponent(JSON.stringify(attr))
					prevPage.onShow(info)
					uni.navigateBack()
				}
			},
			addAddress() { //新增地址信息
				uni.navigateTo({
					url: '/page_mine/EditAddress'
				})
			},
			
			// 点击删除地址
			deleteAddress(addId) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认删除该地址吗？',
					success: async function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							let {data:data} = await that.$myRequest({
								url: '/addressControllerBefore/deleteAddress/'+uni.getStorageSync('userId')+'/'+addId,
								method:"delete",
							})
							if(data.code==20000){
								uni.showToast({
									title:'删除成功',
									icon:'none'
								})
								that.getAddressList()
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		}
	}
</script>
<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		background-color: white;
	}
</style>
<style lang="scss" scoped>
	.content {
		width: 90%;
		margin: 20rpx auto;
		font-size: 30rpx;
		display: flex;

		.left {
			width: 90%;
		}

		.top {
			display: flex;
			line-height: 30rpx;

			.name {
				margin-right: 20rpx;
			}
		}

		.bot {
			margin-top: 10rpx;
			font-size: 24rpx;
			color: lightgray;
		}
	}

	.line {
		height: 1px;
		background-color: lightgray;
		margin-top: 20rpx;
	}

	.delete {
		background-color: red;
	}

	.addAddress {
		margin-left: 12rpx;

		.add {
			display: flex;
			color: #AEB5A5;
			align-items: center;
			font-size: 28rpx;

			/deep/.u-icon {
				margin: 18rpx;
			}

		}
	}
</style>
